home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995…tember: Reference Library / Dev.CD Sep 95 RL / Dev.CD Sep 95 RL.toast / mac / Technical Documentation / develop / develop Issue 14 code / Writing Localizable Apps / Multiple Encodings.c < prev    next >
Encoding:
Text File  |  1993-03-09  |  3.1 KB  |  107 lines  |  [TEXT/KAHL]

  1. /**********************************************************************************
  2.  
  3.     NOTE: This file won't compile as-is, though each sample has been compiled on its
  4.     own. The code here has been extracted from the article simply for your convenience, 
  5.     so you won't have to type it in. Take what you need, and adapt it for your own 
  6.     devious purposes.
  7.     
  8. ***********************************************************************************/
  9.  
  10.  
  11. //--------------------------------------
  12. //    Changing fonts when using the multiple 
  13. //    encodings method in your app.
  14.  
  15. short fontScript;
  16. short textRunIndex, textRunCount;
  17.  
  18. fontScript = Font2Script(myNewFontID);
  19. for (textRunIndex = 0;
  20.         textRunIndex < textRunCount;
  21.         textRunIndex++) {
  22.     if (textRunStore[textRunIndex].script == fontScript)
  23.         textRunStore[textRunIndex].fontID = myNewFontID;
  24.     else
  25.         SpecialProcessing(&textRunIndex, &textRunCount, myNewFontID);
  26. }
  27.  
  28. Boolean FindASCIIRun(unsigned char *textPtr, long textLength,
  29.     long *runLength)
  30. {
  31.     *runLength = 0;
  32.  
  33.     if (*textPtr < 0x80) {
  34.         // We know that this character is simple ASCII, since values less
  35.         // than 128 can't be the first byte of a two-byte character, and
  36.         // they're shared among all scripts. So, let's block up a run of
  37.         // simple ASCII.
  38.         while (*textPtr++ < 0x80 && textLength-- > 0)
  39.             *runLength++;
  40.         return true;            // Run is simple ASCII.
  41.     } else {
  42.         // We know this character is not simple ASCII. It may be two-byte
  43.         // or it may be some character in a non-Roman script. So, let's
  44.         // block up a run of non-simple-ASCII characters.
  45.         while (textLength > 0) {
  46.             if (CharByte(textPtr, 0) == smFirstByte) {
  47.                 // Skip over two-byte character.
  48.                 textPtr += 2;
  49.                 textLength -= 2;
  50.                 *runLength += 2;
  51.             } else {
  52.                 // Skip over one-byte character.
  53.                 textPtr++;
  54.                 textLength--;
  55.                 *runLength++;
  56.             }
  57.         }
  58.         return false;            // Run is NOT simple ASCII.
  59.     }
  60. }
  61.  
  62. void SpecialProcessing(short *runIndex, short *runCount,
  63.     short myNewFontID)
  64. {
  65.     TextRunRecord        originalRun, createdRun;
  66.     unsigned char        *textPtr;
  67.     long                    textLength, runLength, runFollow;
  68.     Boolean                simpleASCII;
  69.  
  70.     // Retrieve this run and remove it from the run list.
  71.     GetTextRun(*runIndex, &originalRun);
  72.     RemoveTextRun(*runIndex);
  73.     
  74.     // Get the pointer and length of the original text.
  75.     textPtr = originalRun.text;
  76.     textLength = originalRun.count;
  77.  
  78.     // Loop through all of the sub-runs in this run.
  79.     runFollow = *runIndex;
  80.     while (textLength > 0) {
  81.         // Find the length of the sub-run and its type.
  82.         TextFont(originalRun.fontID);
  83.         simpleASCII= FindASCIIRun(textPtr, textLength, &runLength);
  84.  
  85.         // Create the sub-run and duplicate the characters.
  86.         createdRun = originalRun;    // Same formats.
  87.         createdRun.text = (unsigned char *)NewPtr(runLength);
  88.         createdRun.length = runLength;
  89.         BlockMove(textPtr, createdRun.text, runLength);
  90.  
  91.         // Roman runs can use the new font.
  92.         if (simpleASCII)
  93.             createdRun.fontID = myNewFontID;
  94.  
  95.         // Add the new sub-run and advance the run index.
  96.         AddTextRun(runFollow++, createdRun);
  97.  
  98.         // Advance over this sub-run and continue looping.
  99.         textPtr += runLength;
  100.         textLength -= runLength;
  101.     }
  102.  
  103.     // Dispose of the original run information.
  104.     DisposeTextRun(originalRun);
  105. }
  106.  
  107.